﻿var ns = namespace("kaskelot.sp.ui");

ns.startbytval = function () {

	function showBytValForm() {
		var selector = "#bytval-form";
		kaskelot.sp.ui.formutils.getAndShowForm({
			params: {
				formName: "BytValForm",
				idString: "",
				parentIdString: ""
			},
			selector: selector,
			openFunction: function () {
				$("#bytval-form").dialog({ title: "Byt val" });
			},
			okFunction: function () {
				var valId = $("#bytval-form #ValId").val();
				if (valId === "")
					return;

				var method = "SetActive";

				$.ajax({
					url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/" + method,
					data: { id: valId },
					type: "GET",
					processData: true,
					contentType: "application/json",
					dataType: "json",
					cache: false,
					success: function (resp) {
						if (resp[method + "Result"].HasError) {
							alert(resp[method + "Result"].ErrorMessage);
						}
						else {
							window.location.reload();
						}
					},
					error: function (xhr) {
						alert(xhr.responseText);
					}
				});
			}
		});
	}

	function getList() {
		$.ajax({
			url: L_Menu_BaseUrl + "/_vti_bin/Kaskelot.SP.Resources/ClientAdminService.svc/GetAllVal",
			type: "GET",
			data: {},
			processData: true,
			cache: false,
			success:
                function (data) {
                	if (!data.GetAllValResult.HasError) {
                		$("#val-results tbody").html(
							$("#val-template").render(data.GetAllValResult.Data)
						);
                	}
                },
			error:
                function (xhr) {
                	kaskelot.sp.ui.prompts.showErrorPrompt(xhr.responseText);
                }
		});
	}

	function setActive(id) {
		$("#bytval-form #ValId").val(id);
		$("#bytval-form tr").removeClass("selected-row");
		$("#bytval-form #" + id).addClass("selected-row");
	}

	return {
		showBytValForm: showBytValForm,
		getList: getList,
		setActive: setActive
	};
}();